<?php

namespace app\home\controller;

use app\log;
use gophp\request;
use gophp\response;
use gophp\validate;

class api extends auth {

    /**
     * 添加接口
     */
    public function add(){

        if(request::isAjax()){

            $api = request::post('api', []);

            $result = \app\api::add($api);
            if(200==$result['code']){
                $api_id = $result['data']['id'];
                $user_id = $result['data']['user_id'];
                $time = date('Y-m-d H:i:s');

                $data1=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'Authorization', 'title'=>'header头', 'type'=>'string', 'method'=>3, 'is_required'=>0, 'default_value'=>'Bearer ***********', 'intro'=>'注意：1，这里的*****就是登录成功后返回给您的 token的值 。2，Bearer 与token 之间有一个空格。', 'mock'=>'' ,'add_time'=>$time];
                $lck1 =  db('field')->show(false)->add($data1);

                $data2=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'data', 'title'=>'用户数据', 'type'=>'string', 'method'=>1, 'is_required'=>1, 'default_value'=>'{"title":"你的内容"}', 'intro'=>'用户请求数据，若无请写 {}', 'mock'=>'' ,'add_time'=>$time];
                $lck2 =  db('field')->show(false)->add($data2);

                $data3=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'timestamp', 'title'=>'时间戳', 'type'=>'string', 'method'=>1, 'is_required'=>1, 'default_value'=>'1539161583', 'intro'=>'请在发起请求时，生成时间戳，超时将不能通过gataway，至请求失败', 'mock'=>'' ,'add_time'=>$time];
                $lck3 =  db('field')->show(false)->add($data3);

                $data4=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'microsrv_method', 'title'=>'请求服务', 'type'=>'string', 'method'=>1, 'is_required'=>1, 'default_value'=>'sysadmin.carstateadd', 'intro'=>'请求服务.方法', 'mock'=>'' ,'add_time'=>$time];
                $lck4 =  db('field')->show(false)->add($data4);

                $data5=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'v', 'title'=>'api 版本', 'type'=>'string', 'method'=>1, 'is_required'=>1, 'default_value'=>'1.0.0', 'intro'=>'api版本默认 1.0.0', 'mock'=>'' ,'add_time'=>$time];
                $lck5 =  db('field')->show(false)->add($data5);

                $data6=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'dev_id', 'title'=>'设备ID', 'type'=>'string', 'method'=>1, 'is_required'=>1, 'default_value'=>'0', 'intro'=>'手机端请获取用户设备的ID，其他端请获MAC地址。没有请填0', 'mock'=>'' ,'add_time'=>$time];
                $lck6 =  db('field')->show(false)->add($data6);

                $data7=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'app_id', 'title'=>'不同app特定的值', 'type'=>'string', 'method'=>1, 'is_required'=>1, 'default_value'=>'BOSS_ERP', 'intro'=>'不用应用固定值,请查看相关文档', 'mock'=>'' ,'add_time'=>$time];
                $lck7 =  db('field')->show(false)->add($data7);

                $data8=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'signature', 'title'=>'签名', 'type'=>'string', 'method'=>1, 'is_required'=>1, 'default_value'=>'0411da2352ca7c9124dfe208fb9dc081', 'intro'=>'由以上参数按一定规律加密生成。', 'mock'=>'' ,'add_time'=>$time];
                $lck8 =  db('field')->show(false)->add($data8);

                $data9=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'code', 'title'=>'状态', 'type'=>'string', 'method'=>2, 'is_required'=>0, 'default_value'=>'', 'intro'=>'"code": 0,', 'mock'=>'' ,'add_time'=>$time];
                $lck9 =  db('field')->show(false)->add($data9);

                $data10=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'msg', 'title'=>'返回提示内容', 'type'=>'string', 'method'=>2, 'is_required'=>0, 'default_value'=>'', 'intro'=>'"msg": "添加车辆-状态成功",', 'mock'=>'' ,'add_time'=>$time];
                $lck10 =  db('field')->show(false)->add($data10);

                $data11=['api_id'=>$api_id, 'user_id'=>$user_id, 'parent_id'=>0, 'name'=>'data  ', 'title'=>'返回数据', 'type'=>'array', 'method'=>2, 'is_required'=>0, 'default_value'=>'', 'intro'=>'请添加data下的子参数', 'mock'=>'' ,'add_time'=>$time];
                $lck11 =  db('field')->show(false)->add($data11);

                if($lck1&&$lck2&&$lck3&&$lck4&&$lck5&&$lck6&&$lck7&&$lck8&&$lck9&&$lck10&&$lck11){
                    response::ajax(['code' => 200, 'msg' => '接口添加成功,参数已生成']);
                }else{
                    response::ajax(['code' => 200, 'msg' => '接口添加成功']);
                }
            }else{
                response::ajax(['code' => $result['code'], 'msg' => $result['msg']]);
            }



            //response::ajax(['code' => $result['code'], 'msg' => $result['msg']]);

        }else{

            $module_id = get('id', 0);

            $module = _uri('module', $module_id);
            $this->assign('module', $module);
            // $user_id = \user_helper::get_user_id();
            // $data1=['api_id'=>88,'user_id'=>999,'parent_id'=>0];
            // var_dump($data1);
            // $lck =  db('field')->show(false)->add($data1);
            // echo "--------------------------------------------";
            // var_dump($lck);
            $this->display('api/add');

        }

    }

    /** 
     * 删除接口
     */
    public function delete()
    {

        $api_id   = request::post('id', 0);
        $password = request::post('password', '');

        $api = \app\api::get_api_info($api_id);

        if(!$api){

            response::ajax(['code' => 301, 'msg' => '请选择要删除的接口!']);

        }

        if(!\app\user::check_password($password)){

            response::ajax(['code' => 302, 'msg' => '抱歉，密码验证失败!']);

        }

        $result  = db('api')->show(false)->delete($api_id);

        $project = \app\api::get_project_info($api_id);

        $module  = \app\module::get_module_info($api['module_id']);

        if($result){

            // 记录日志
            $log = [
                'project_id' => $project['id'],
                'type'       => '删除',
                'object'     => '接口',
                'content'    => '删除模块<code>'.$module['title'].'</code>下的接口<code>' . $api['title'] . '</code>',
            ];

            log::project($log);

            response::ajax(['code' => 200, 'msg' => '删除成功!']);

        }else{

            response::ajax(['code' => 403, 'msg' => '删除失败!']);

        }

    }

    /**
     * 编辑接口
     */
    public function edit()
    {

        $api_id = request::post('id', 0);

        // 判断接口是否存在
        $api = _uri('api', $api_id);

        if(!$api){

            $this->error('抱歉，该接口不存在');

        }

        $project_id = _uri('module', $api['module_id'], 'project_id');

        $modules = db('module')->where('project_id', '=', $project_id)->findAll();

        // 获取请求参数列表
        $request_fields = \app\field::get_field_list($api_id, 1);

        // 获取响应参数列表
        $response_fields = \app\field::get_field_list($api_id, 2);

        // 获取header参数列表
        $header_fields = \app\field::get_field_list($api_id, 3);

        // 获取返回json示例
        $respose_json = json_encode(\app\field::get_default_data($api_id), JSON_UNESCAPED_UNICODE);

        $this->assign('api', $api);
        $this->assign('modules', $modules);
        $this->assign('request_fields', $request_fields);
        $this->assign('response_fields', $response_fields);
        $this->assign('header_fields', $header_fields);
        $this->assign('respose_json', $respose_json);

        $this->display('api/edit');

    }


    public function load()
    {

        $api_id = request::post('id', 0);

        $api    = _uri('api', $api_id);

        // 判断接口是否存在
        if(!$api){

            $this->error('抱歉，该接口不存在');

        }

        $api['module'] = _uri('module', $api['module_id']);

        $project_id = _uri('module', $api['module_id'], 'project_id');

        $project = _uri('project', $project_id);

        // 获取项目模块
        $modules = db('module')->where('project_id', '=', $project_id)->findAll();

        // 获取请求参数列表
        $request_fields = \app\field::get_field_list($api_id, 1);

        // 获取响应参数列表
        $response_fields = \app\field::get_field_list($api_id, 2);

        // 获取header参数列表
        $header_fields = \app\field::get_field_list($api_id, 3);

        $this->assign('api', $api);
        $this->assign('project', $project);
        $this->assign('modules', $modules);
        $this->assign('request_fields', $request_fields);
        $this->assign('response_fields', $response_fields);
        $this->assign('header_fields', $header_fields);

        $this->display('api/load');

    }

    /**
     * 接口详情
     * @param $id
     * @param $arguments
     */
    public function __call($id, $arguments)
    {

        $api_id = id_decode($id) ? id_decode($id) : 0;
        $api    = \app\api::get_api_info($api_id);

        // 判断接口是否存在
        if(!$api){

            $this->error('抱歉，该接口不存在');

        }

        $api['decode_id'] = $id;

        $api['module'] = _uri('module', $api['module_id']);

        $project_id    = _uri('module', $api['module_id'], 'project_id');

        $project       = _uri('project', $project_id);
        $project['encode_id'] = id_encode($project_id);

        if(!\app\member::has_rule($project_id, 'api', 'look')){

            $this->error('抱歉，您无权查看该接口');

        }

        $envs    = json_decode($project['envs'], true);

        // 获取项目模块
        $modules = db('module')->where('project_id', '=', $project_id)->findAll();

        // 获取header参数列表
        $header_fields = \app\field::get_field_list($api_id, 3);

        // 获取请求参数列表
        $request_fields = \app\field::get_field_list($api_id, 1);

        // 获取响应参数列表
        $response_fields = \app\field::get_field_list($api_id, 2);

        // 获取返回json示例
        $respose_json = json_encode(\app\field::get_default_data($api_id), JSON_UNESCAPED_UNICODE);

        $this->assign('api', $api);
        $this->assign('project', $project);
        $this->assign('envs', $envs);
        $this->assign('modules', $modules);
        $this->assign('header_fields', $header_fields);
        $this->assign('request_fields', $request_fields);
        $this->assign('response_fields', $response_fields);
        $this->assign('respose_json', $respose_json);

        $this->display('api/detail');

    }
}